#include "cellui.h"
#include "ui_cellui.h"
#include "constants.h"
#include "infoui.h"
#include "paramui.h"
#include <QTimer>
#include <QDateTime>
#include <QFile>
#include <QTextStream>

QList<QStringList> CellUI::qData;

CellUI::CellUI(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CellUI),
    iCellType(-1)
{
    ui->setupUi(this);
    setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint );
    //load logo and background
    QPixmap lg(logoStr);
    ui->logo->setPixmap(lg);
    QPixmap bg(backgroundStr);
    QPalette background;
    background.setBrush(QPalette::Background, QBrush(bg));
    setPalette(background);
    //set font color
    QPalette pe;
    pe.setColor(QPalette::WindowText,Qt::white);
    ui->cTime->setPalette(pe);
    ui->title->setPalette(pe);
    ui->tDianya->setPalette(pe);
    ui->tMaikuan->setPalette(pe);
    ui->tCishu->setPalette(pe);
    ui->tJiange->setPalette(pe);
    ui->tLiusu->setPalette(pe);
    ui->tYaodong->setPalette(pe);

    //button style
    ui->btnRet->setStyleSheet(redBtn);
    ui->btnOk->setStyleSheet(blueBtn);
    ui->btnCus->setStyleSheet(greenBtn);

    QPalette btnPe;
    btnPe.setColor(QPalette::ButtonText,Qt::white);
    ui->btnRet->setPalette(btnPe);
    ui->btnOk->setStyleSheet(blueBtn);
    ui->btnCus->setStyleSheet(greenBtn);


    ui->nkCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->cusCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->tCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->bCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->cikCel->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->dcCell->setStyleSheet("background-color: grey; border-radius: 40px;");

    ui->subOne->setStyleSheet("background-color: grey; border-radius: 25px;");
    ui->subOne->setVisible(false);
    ui->subTwo->setStyleSheet("background-color: grey; border-radius: 25px;");
    ui->subTwo->setVisible(false);
    ui->subThree->setStyleSheet("background-color: grey; border-radius: 25px;");
    ui->subThree->setVisible(false);


    btnPe.setColor(QPalette::ButtonText, Qt::white);
    ui->nkCell->setPalette(btnPe);
    ui->cusCell->setPalette(btnPe);
    ui->tCell->setPalette(btnPe);
    ui->bCell->setPalette(btnPe);
    ui->cikCel->setPalette(btnPe);
    ui->dcCell->setPalette(btnPe);
    ui->subOne->setPalette(btnPe);
    ui->subTwo->setPalette(btnPe);
    ui->subThree->setPalette(btnPe);

    ui->nkCell->setFocusPolicy(Qt::NoFocus);
    ui->cusCell->setFocusPolicy(Qt::NoFocus);
    ui->tCell->setFocusPolicy(Qt::NoFocus);
    ui->bCell->setFocusPolicy(Qt::NoFocus);
    ui->cikCel->setFocusPolicy(Qt::NoFocus);
    ui->dcCell->setFocusPolicy(Qt::NoFocus);
    ui->subOne->setFocusPolicy(Qt::NoFocus);
    ui->subTwo->setFocusPolicy(Qt::NoFocus);
    ui->subThree->setFocusPolicy(Qt::NoFocus);

    //text edit color
    QPalette pal;
    pal.setColor(QPalette::Text, Qt::black);
    ui->tDianya->setPalette(pal);
    ui->tCishu->setPalette(pal);
    ui->tJiange->setPalette(pal);
    ui->tMaikuan->setPalette(pal);
    ui->tYaodong->setPalette(pal);
    ui->tLiusu->setPalette(pal);

    QTimer *upTime = new QTimer(this);
    connect(upTime,SIGNAL(timeout()),this,SLOT(updateTime()));
    updateTime();
    upTime->start(1000);
}

CellUI::~CellUI()
{
    delete ui;
}

bool CellUI::bParamExist(QString cellStr)
{
    bool bExist = false;
    for (int i=0; i<qData.size(); i++)
    {
        QString cellTmp = qData.at(i).first();
        if (cellTmp.compare(cellStr, Qt::CaseInsensitive) == 0)
        {
            bExist = true;
            break;
        }
    }

    return bExist;
}

void CellUI::updateTime()
{
    QDateTime curTime = QDateTime::currentDateTime();
    QString lblTm = curTime.date().toString("yyyy/MM/dd") + "\n " + curTime.time().toString("hh:mm:ss");
    this->ui->cTime->setText(lblTm);
}

void CellUI::getShow()
{
    this->show();
    this->activateWindow();
    this->raise();
    //set all
    ui->tCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->nkCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->cusCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->bCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->cikCel->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->dcCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->subOne->setVisible(false);
    ui->subTwo->setVisible(false);
    ui->subThree->setVisible(false);
    ui->subOne->setStyleSheet("background-color: grey; border-radius: 25px;");
    ui->subTwo->setStyleSheet("background-color: grey; border-radius: 25px;");
    ui->subThree->setStyleSheet("background-color: grey; border-radius: 25px;");
    clearParams();
    qData.clear();
    //init parameters list
    QFile f1("/home/root/params.txt");
    f1.open(QIODevice::ReadOnly);
    QTextStream s1(&f1);
    while (!s1.atEnd()) {
      QString s=s1.readLine(); // reads line from file
      qData.push_back(s.split(","));
    }
    f1.close();
    //load custom parameter
    QFile f2("/home/root/cus.txt");
    f2.open(QIODevice::ReadOnly);
    QTextStream s2(&f2);
    while (!s2.atEnd()) {
      QString s=s2.readLine(); // reads line from file
      qData.push_back(s.split(","));
    }
    f2.close();
}

void CellUI::on_btnRet_clicked()
{
    this->hide();
    emit retMain();
}

void CellUI::on_btnSave_clicked()
{

}

void CellUI::on_btnOk_clicked()
{
    //check param
    if (ParamUI::qstrDianya.length() <= 0
            || ParamUI::qstrMaikuan.length() <=0
            || ParamUI::qstrCishu.length() <=0
            || ParamUI::qstrJiange.length() <=0
            || ParamUI::qstrLiusu.length() <=0
            || ParamUI::qstrCell.length() <=0 )
    {
        InfoUI ifu;
        ifu.InfoDialog();
        ifu.SetInfo(tr("No parameter"), tr("No Prameters\n  Please re-select!"));
        ifu.exec();
        return;
    }
    //clear saved staffs
    ParamUI::qstrName = "";
    ParamUI::qstrBarcode = "";
    ParamUI::qstrId = "";

    this->hide();
    emit paramSet(0);
}

void CellUI::on_btnCus_clicked()
{
    this->hide();
    emit onCustom();
}

void CellUI::loadParameters(QString qCell)
{
    //empty all staffs
    ui->tDianya->setText(tr(""));
    ui->tMaikuan->setText(tr(""));
    ui->tCishu->setText(tr(""));
    ui->tJiange->setText(tr(""));
    ui->tLiusu->setText(tr(""));
    ui->tYaodong->setText(tr(""));
    bool bFound = false;
    QStringList slTmp;
    for (int i=0; i<qData.size(); i++)
    {
        QString cellTmp = qData.at(i).first();
        if (cellTmp.compare(qCell, Qt::CaseInsensitive) == 0)
        {
            slTmp = qData.at(i);
            bFound = true;
            break;
        }
    }

    if (!bFound)
    {
        return;
    }
    ParamUI::qstrDianya = slTmp[1];
    ParamUI::qstrMaikuan = slTmp[2];
    ParamUI::qstrCishu = slTmp[3];
    ParamUI::qstrJiange = slTmp[4];
    ParamUI::qstrLiusu = slTmp[5];
    ParamUI::qstrCell = qCell;

    ui->tDianya->setText(ParamUI::qstrDianya);
    ui->tMaikuan->setText(ParamUI::qstrMaikuan);
    ui->tCishu->setText(ParamUI::qstrCishu);
    ui->tJiange->setText(ParamUI::qstrJiange);
    ui->tLiusu->setText(ParamUI::qstrLiusu);
    if (slTmp[6].compare("1") == 0)
    {
        ParamUI::blYaodong = true;
        ui->tYaodong->setText(tr("On"));
    }
    else if (slTmp[6].compare("0") == 0)
    {
        ParamUI::blYaodong = false;
        ui->tYaodong->setText(tr("Off"));
    }
}

void CellUI::clearParams()
{
    ui->tDianya->setText("");
    ui->tMaikuan->setText("");
    ui->tCishu->setText("");
    ui->tJiange->setText("");
    ui->tLiusu->setText("");
    ui->tYaodong->setText("");

    ParamUI::qstrDianya = "";
    ParamUI::qstrMaikuan = "";
    ParamUI::qstrCishu = "";
    ParamUI::qstrJiange = "";
    ParamUI::qstrLiusu = "";
    ParamUI::qstrCell = "";
}

void CellUI::on_tCell_clicked()
{
    ui->tCell->setStyleSheet("background-color: blue; border-radius: 40px;");
    ui->nkCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->cusCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->bCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->cikCel->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->dcCell->setStyleSheet("background-color: grey; border-radius: 40px;");

    ui->subGrp->setGeometry(50,180,440,70);
    ui->subOne->setText(tr("T1"));
    ui->subOne->setVisible(true);
    ui->subTwo->setText(tr("T2"));
    ui->subTwo->setVisible(true);
    ui->subThree->setText(tr("T3"));
    ui->subThree->setVisible(true);
    iCellType = TCELL;

    ui->subOne->setStyleSheet("background-color: grey; border-radius: 25px;");
    ui->subTwo->setStyleSheet("background-color: grey; border-radius: 25px;");
    ui->subThree->setStyleSheet("background-color: grey; border-radius: 25px;");
    clearParams();
}

void CellUI::on_nkCell_clicked()
{
    ui->nkCell->setStyleSheet("background-color: blue; border-radius: 40px;");
    ui->cusCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->tCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->bCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->cikCel->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->dcCell->setStyleSheet("background-color: grey; border-radius: 40px;");

    ui->subGrp->setGeometry(50,180,440,70);
    ui->subOne->setText(tr("NK1"));
    ui->subOne->setVisible(true);
    ui->subTwo->setText(tr("NK2"));
    ui->subTwo->setVisible(true);
    ui->subThree->setText(tr("NK3"));
    ui->subThree->setVisible(true);
    iCellType = NKCELL;

    ui->subOne->setStyleSheet("background-color: grey; border-radius: 25px;");
    ui->subTwo->setStyleSheet("background-color: grey; border-radius: 25px;");
    ui->subThree->setStyleSheet("background-color: grey; border-radius: 25px;");
    clearParams();
}

void CellUI::on_bCell_clicked()
{
    ui->bCell->setStyleSheet("background-color: blue; border-radius: 40px;");
    ui->nkCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->cusCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->tCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->cikCel->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->dcCell->setStyleSheet("background-color: grey; border-radius: 40px;");

    ui->subGrp->setGeometry(210,180,440,70);
    ui->subOne->setText(tr("B1"));
    ui->subOne->setVisible(true);
    ui->subTwo->setText(tr("B2"));
    ui->subTwo->setVisible(true);
    ui->subThree->setText(tr("B3"));
    ui->subThree->setVisible(true);
    iCellType = BCELL;

    ui->subOne->setStyleSheet("background-color: grey; border-radius: 25px;");
    ui->subTwo->setStyleSheet("background-color: grey; border-radius: 25px;");
    ui->subThree->setStyleSheet("background-color: grey; border-radius: 25px;");
    clearParams();
}

void CellUI::on_dcCell_clicked()
{
    ui->dcCell->setStyleSheet("background-color: blue; border-radius: 40px;");
    ui->nkCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->cusCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->tCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->bCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->cikCel->setStyleSheet("background-color: grey; border-radius: 40px;");

    ui->subGrp->setGeometry(370,180,440,70);
    ui->subOne->setText(tr("DC1"));
    ui->subOne->setVisible(true);
    ui->subTwo->setText(tr("DC2"));
    ui->subTwo->setVisible(true);
    ui->subThree->setText(tr("DC3"));
    ui->subThree->setVisible(true);
    iCellType = DCCELL;

    ui->subOne->setStyleSheet("background-color: grey; border-radius: 25px;");
    ui->subTwo->setStyleSheet("background-color: grey; border-radius: 25px;");
    ui->subThree->setStyleSheet("background-color: grey; border-radius: 25px;");
    clearParams();
}

void CellUI::on_cikCel_clicked()
{
    ui->cikCel->setStyleSheet("background-color: blue; border-radius: 40px;");
    ui->nkCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->cusCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->tCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->bCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->dcCell->setStyleSheet("background-color: grey; border-radius: 40px;");

    ui->subGrp->setGeometry(530,180,440,70);
    ui->subOne->setText(tr("CIK1"));
    ui->subOne->setVisible(true);
    ui->subTwo->setText(tr("CIK2"));
    ui->subTwo->setVisible(true);
    ui->subThree->setText(tr("CIK3"));
    ui->subThree->setVisible(true);
    iCellType = CIKCELL;

    ui->subOne->setStyleSheet("background-color: grey; border-radius: 25px;");
    ui->subTwo->setStyleSheet("background-color: grey; border-radius: 25px;");
    ui->subThree->setStyleSheet("background-color: grey; border-radius: 25px;");
    clearParams();
}

void CellUI::on_cusCell_clicked()
{
    ui->cusCell->setStyleSheet("background-color: blue; border-radius: 40px;");
    ui->nkCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->tCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->bCell->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->cikCel->setStyleSheet("background-color: grey; border-radius: 40px;");
    ui->dcCell->setStyleSheet("background-color: grey; border-radius: 40px;");

    ui->subGrp->setGeometry(530,180,440,70);
    ui->subOne->setText(tr("CUS1"));
    ui->subOne->setVisible(true);
    ui->subTwo->setText(tr("CUS2"));
    ui->subTwo->setVisible(true);
    ui->subThree->setText(tr("CUS3"));
    ui->subThree->setVisible(true);

    iCellType = CUSCELL;

    ui->subOne->setStyleSheet("background-color: grey; border-radius: 25px;");
    ui->subTwo->setStyleSheet("background-color: grey; border-radius: 25px;");
    ui->subThree->setStyleSheet("background-color: grey; border-radius: 25px;");
    clearParams();
}

void CellUI::on_subOne_clicked()
{
    if (iCellType == -1)
    {
        return;
    }
    ui->subOne->setStyleSheet("background-color: blue; border-radius: 25px;");
    ui->subTwo->setStyleSheet("background-color: grey; border-radius: 25px;");
    ui->subThree->setStyleSheet("background-color: grey; border-radius: 25px;");
    switch (iCellType) {
    case TCELL:
        loadParameters("T1");
        break;
    case BCELL:
        loadParameters("B1");
        break;
    case NKCELL:
        loadParameters("NK1");
        break;
    case DCCELL:
        loadParameters("DC1");
        break;
    case CIKCELL:
        loadParameters("CIK1");
        break;
    case CUSCELL:
        loadParameters("CUS1");
        break;
    default:
        break;
    }

}

void CellUI::on_subTwo_clicked()
{
    if (iCellType == -1)
    {
        return;
    }
    ui->subOne->setStyleSheet("background-color: grey; border-radius: 25px;");
    ui->subTwo->setStyleSheet("background-color: blue; border-radius: 25px;");
    ui->subThree->setStyleSheet("background-color: grey; border-radius: 25px;");
    switch (iCellType) {
    case TCELL:
        loadParameters("T2");
        break;
    case BCELL:
        loadParameters("B2");
        break;
    case NKCELL:
        loadParameters("NK2");
        break;
    case DCCELL:
        loadParameters("DC2");
        break;
    case CIKCELL:
        loadParameters("CIK2");
        break;
    case CUSCELL:
        loadParameters("CUS2");
        break;
    default:
        break;
    }
}

void CellUI::on_subThree_clicked()
{
    if (iCellType == -1)
    {
        return;
    }
    ui->subOne->setStyleSheet("background-color: grey; border-radius: 25px;");
    ui->subTwo->setStyleSheet("background-color: grey; border-radius: 25px;");
    ui->subThree->setStyleSheet("background-color: blue; border-radius: 25px;");
    switch (iCellType) {
    case TCELL:
        loadParameters("T3");
        break;
    case BCELL:
        loadParameters("B3");
        break;
    case NKCELL:
        loadParameters("NK3");
        break;
    case DCCELL:
        loadParameters("DC3");
        break;
    case CIKCELL:
        loadParameters("CIK3");
        break;
    case CUSCELL:
        loadParameters("CUS3");
        break;
    default:
        break;
    }
}
